package web

import (
	"fmt"

	"gitee.com/youkelike/zlog"
	"gitee.com/youkelike/zmigrate/example/interactive/service"
	"gitee.com/youkelike/zmigrate/pkg/ginx"
	"github.com/gin-gonic/gin"
)

type InteractiveHandler struct {
	svc service.InteractiveService
	l   zlog.Logger
}

func NewInteractiveHandler(svc service.InteractiveService, l zlog.Logger) *InteractiveHandler {
	return &InteractiveHandler{
		svc: svc,
		l:   l,
	}
}

type IncrReadCntReq struct {
	Biz   string `form:"biz" binding:"required"`
	BizId int64  `form:"biz_id" binding:"required"`
}
type LikeReq struct {
	Biz   string `form:"biz" binding:"required"`
	BizId int64  `form:"biz_id" binding:"required"`
	Uid   int64  `form:"uid" binding:"required"`
}

func (i *InteractiveHandler) RegisterRoutes(server *gin.RouterGroup) {
	server.GET("/incr_read_cnt", ginx.WrapBody[IncrReadCntReq](i.l, i.IncrReadCnt))
	server.GET("/like", ginx.WrapBody[LikeReq](i.l, i.Like))
}

func (i *InteractiveHandler) IncrReadCnt(ctx *gin.Context, req IncrReadCntReq) (ginx.Result, error) {
	err := i.svc.IncrReadCnt(ctx.Request.Context(), req.Biz, req.BizId)
	if err != nil {
		return ginx.Result{
			Msg: fmt.Sprintf("操作失败: %v", err),
		}, nil
	}
	return ginx.Result{
		Msg: "OK",
	}, nil
}

func (i *InteractiveHandler) Like(ctx *gin.Context, req LikeReq) (ginx.Result, error) {
	err := i.svc.Like(ctx.Request.Context(), req.Biz, req.BizId, req.Uid)
	if err != nil {
		return ginx.Result{
			Msg: fmt.Sprintf("操作失败: %v", err),
		}, nil
	}
	return ginx.Result{
		Msg: "OK",
	}, nil

}
